package cn.outmaneuver.outman.demo.web.controller;
/**
 * Copyright 2023-2023 by DongYaoMing. All Rights Reserved.
 */

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.Arrays;

/**
 * BasicDemoController
 * <p>基础demo控制类</p>
 *
 * @author DongYaoMing
 * @version 1.0
 * @date 2023/09/13
 */
@Controller
@RequestMapping("/demo/basic")
public class BasicDemoController {

    /**
     * rest接口示例
     * <p>本例为最简单的rest接口，更详细的请阅读如下内容 :{@link RequestDemoController} ；{@link ResponseDemoController}</p>
     *
     * @return 返回前端的结果
     */
    @RequestMapping("/demoRest")
    @ResponseBody
    public String demoRest() {
        return "hello ,I`m a demo of rest";
    }


    /**
     * 页面示例
     * <p>本例使用thymeleaf作为模版，用于展示springboot-web页面开发逻辑，具体thymeleaf标签用法，后续会逐步完善。</p>
     *
     * @return 数据及页面
     */
    @RequestMapping("/demoView")
    public ModelAndView demoView(@ModelAttribute("name") String name) {
        ModelAndView modelAndView = new ModelAndView();
        if (StringUtils.isEmpty(name)) {
            name = "thymeleaf";
        }
        modelAndView.getModelMap().addAttribute("name", name);
        modelAndView.getModelMap().addAttribute("books", Arrays.asList("三国演义", "水浒传", "西游记", "红楼梦"));
        modelAndView.setViewName("/views/thmeleafDemo");
        return modelAndView;
    }

    /**
     * 页面示例2
     * <p>本例使用thymeleaf作为模版，用于展示springboot-web页面开发逻辑，具体thymeleaf标签用法，后续会逐步完善。</p>
     *
     * @return 数据及页面
     */
    @RequestMapping("/demoView2")
    public String demoView2(ModelMap modelMap, @ModelAttribute("name") String name) {
        if (StringUtils.isEmpty(name)) {
            name = "thymeleaf";
        }
        modelMap.addAttribute("name", name);
        modelMap.addAttribute("books", Arrays.asList("三国演义", "水浒传", "西游记", "红楼梦"));
        return "/views/thmeleafDemo";
    }

    /**
     * 重定向示例
     * <p>展示不同的重定向方式。根据name的值重定向到不同路径，name无值时，重定向到外部页面[百度]；name有值时重定向到内部页面[demoView]，name=Implicit时，隐藏参数；</p>
     *
     * @return 数据及页面
     */
    @RequestMapping("/demoRedirect")
    public ModelAndView demoRedirect(String name, RedirectAttributes attr) {
        if (!StringUtils.isEmpty(name)) {
            ModelAndView modelAndView = new ModelAndView();
            if ("Implicit".equals(name)) {
                attr.addFlashAttribute("name", name);
            } else {
                attr.addAttribute("name", name);
            }
            modelAndView.setViewName("redirect:/demo/basic/demoView");
            return modelAndView;
        }
        return new ModelAndView("redirect:https://baidu.com");
    }


    /**
     * 重定向示例2
     * <p>展示不同的重定向方式。根据name的值重定向到不同路径，name无值时，重定向到外部页面[百度]；name有值时重定向到内部页面[demoView]。</p>
     *
     * @return 数据及页面
     */
    @RequestMapping("/demoRedirect2")
    public String demoRedirect2(String name) {
        if (!StringUtils.isEmpty(name)) {
            return "redirect:/demo/basic/demoView?name=" + name;
        }
        return "redirect:https://baidu.com";
    }

}
